﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;

namespace QLNS_v1.Form
{
    public partial class frmChamCong : DevExpress.XtraEditors.XtraForm   
    {
        Bus.NhanVien_Bus nv = new Bus.NhanVien_Bus();
        Bus.ChamCong_Bus cc = new Bus.ChamCong_Bus();
       
        public frmChamCong()
        {
            InitializeComponent();

            colMaChamCong.Fixed = FixedStyle.Left;
            colMaNhanVien.Fixed = FixedStyle.Left;
            colNam.Fixed = FixedStyle.Left;
            colThang.Fixed = FixedStyle.Left;
            colHoTen.Fixed = FixedStyle.Left;
            colTongNgayCong.Fixed = FixedStyle.Right;
            
            cboThang.EditValue = DateTime.Now.Month;
            calNam.EditValue = DateTime.Now.Year;
            cboThang.EditValueChanged+=cboThang_EditValueChanged;
            calNam.EditValueChanged+=calNam_EditValueChanged;
            gridNhanVien.DataSource = nv.ListNhanVien();
            gridChamCong.DataSource = cc.GetChamCong(DateTime.Now.Month, DateTime.Now.Year); 
        }

        private void calNam_EditValueChanged(object sender, EventArgs e)
        {
            gridChamCong.DataSource = cc.GetChamCong(int.Parse(cboThang.EditValue.ToString()), int.Parse(calNam.EditValue.ToString()));

        }

        private void cboThang_EditValueChanged(object sender, EventArgs e)
        {
            gridChamCong.DataSource = cc.GetChamCong(int.Parse(cboThang.EditValue.ToString()),int.Parse(calNam.EditValue.ToString()));
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try{
                gridChamCong.DataSource = cc.ListChamCong_TheoNhanVien(int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NhanVienID").ToString()));
            }catch{

            }
             
        }
        private void gridView2_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.Name != "colTongNgayCong")
            {
                cc.UpdateChamCong(gridView2.GetDataRow(e.RowHandle), (bool)(sender as DevExpress.XtraGrid.Views.Grid.GridView).EditingValue);
                if ((bool)(sender as DevExpress.XtraGrid.Views.Grid.GridView).EditingValue == true)
                {
                    gridView2.SetFocusedRowCellValue(colTongNgayCong, int.Parse(gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "SoNgayDiLam").ToString()) + 1);
                }
                else
                    gridView2.SetFocusedRowCellValue(colTongNgayCong, int.Parse(gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "SoNgayDiLam").ToString()) - 1);

            }
            else return;
            
            
        }
    }
}